package cn.bearspark.api.controller.main;

import cn.bearspark.api.service.main.FileService;
import cn.bearspark.framework.web.starter.vo.ResponseVO;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件模块
 *
 * @author f
 */
@CrossOrigin
@RestController
@RequestMapping("/file")
public class FileController {

    private final FileService fileService;

    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    /**
     * 上传文件
     *
     * @param file       文件
     * @param moduleCode 模块的枚举值
     * @return 携带文件名称的响应
     */
    @PostMapping("/upload/{moduleCode}")
    public ResponseVO<String> upload(MultipartFile file, @PathVariable Integer moduleCode) {
        return fileService.upload(file, moduleCode);
    }

    /**
     * 下载文件
     *
     * @param filename 文件名
     * @return 携带文件的响应
     */
    @GetMapping("/download/{filename}")
    public ResponseEntity<Resource> download(@PathVariable String filename) {
        return fileService.download(filename);
    }
}
